﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Utils;
using System.Data.SqlClient;
using Settings;

namespace SMSROUTER.Business
{
    public class SmsInStatisticStruct
    {
        public string Status { get; set; }
        public Guid? SmsInGUID { get; set; }
        public int? RetryOnFailReqTimes { get; set; }
        public int? RetryOnTimeOutReqTimes { get; set; }
        public long? RetryOnFailReqDuration { get; set; }
        public long? RetryOnTimeOutReqDuration { get; set; }
        public int? ThreadExecute { get; set; }
        public int? RouterID { get; set; }
        public DateTime? LastTimeProcess { get; set; }

        public SmsInStatisticStruct() {} // *Bắt buộc phải có hàm tạo ko đối số thì xml mới serialize được

        public SmsInStatisticStruct(string status, Guid? smsInGUID, int? retryOnFailReqTimes, int? retryOnTimeOutReqTimes, long? retryOnFailReqDuration, long? retryOnTimeOutReqDuration, int? threadExecute, int? routerID, DateTime? lastTimeProcess)
        {
            this.Status = status;
            this.SmsInGUID = smsInGUID;
            this.RetryOnFailReqTimes = retryOnFailReqTimes;
            this.RetryOnTimeOutReqTimes = retryOnTimeOutReqTimes;
            this.RetryOnFailReqDuration = retryOnFailReqDuration;
            this.RetryOnTimeOutReqDuration = retryOnTimeOutReqDuration;
            this.ThreadExecute = threadExecute;
            this.RouterID = routerID;
            this.LastTimeProcess = lastTimeProcess;
        }

        public static IList<SmsInStatisticStruct> CreateList(IList<SmsInWaiting> smsList, DateTime sentTime, string status, int? threadId)
        {
            IList<SmsInStatisticStruct> retList = new List<SmsInStatisticStruct>();
            if (smsList == null)
                return retList;
            foreach (var item in smsList)
            {
                retList.Add(new SmsInStatisticStruct(status, item.SmsInGUID, item.RetryOnFailReqTimes, item.RetryOnTimeOutReqTimes, 
                    item.RetryOnFailReqDuration, item.RetryOnTimeOutReqDuration, threadId, item.RouterID, sentTime));
            }
            return retList;
        }

        public static void BatchUpdate(IList<SmsInStatisticStruct> list)
        {
            if (list.Count > 0)
            {
                try
                {
                    DataAccess.BatchExecute<SmsInStatisticStruct>(list, "@SmsInStatistic", "SmsInStatisticStructType", "usp_SmsInStatistic_Update");
                }
                catch (Exception ex)
                {
                    Log.InsertLog(ex, null, AppSetting.WarningType.mo_error, null);
                    try
                    {
                        Xml.WriteListToFile<SmsInStatisticStruct>(list, AppSetting.SmsInStatisticBackupFileName);
                    }
                    catch (Exception ex1)
                    {
                        Log.InsertLog("SmsInStatistic Write to Xml has fail|" + ex1.Message, ex1.StackTrace, ex1.Source, AppSetting.WarningType.mo_error, null);
                        //throw new Exception();
                    }
                }
            }
        }
    }
}
